package com.atguigu.cmsservice.controller;

import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.entity.vo.BannerQuery;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cmsservice/bannerAdmin")
//@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    //add
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.save(crmBanner);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //delete
    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBanner(@PathVariable String id){
        boolean flag = crmBannerService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //update
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.updateById(crmBanner);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //getById
    @GetMapping("/getBannerById/{id}")
    public R getBanner(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("item", crmBanner);
    }

    //pageQuery
    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page,
                        @PathVariable Long limit,
                        @RequestBody(required = false) BannerQuery bannerQuery){
        Page<CrmBanner> bannerPage = new Page<CrmBanner>(page, limit);
        crmBannerService.pageQuery(bannerPage, bannerQuery);
        List<CrmBanner> records = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        return R.ok().data("rows", records).data("total", total);
    }

}
